local skill = fk.CreateSkill{
  name = "tea__chizong",
}
---@type ActiveSkillSpec
local chizong = {
  anim_type = "support",
  interaction = function(self)
    local all_choices = {"chizong_recover", "chizong_drawcards"}
    local choices = {}
    if Self:getMark("tea__chizong_ma-turn") == 0 then
      table.insert(choices, "chizong_recover")
    end
    if Self:getMark("tea__chizong_mb-turn") == 0 then
      table.insert(choices, "chizong_drawcards")
    end
    return UI.ComboBox { choices = choices , all_choices = all_choices }
  end,
  target_num = 1,
  can_use = function(self, player)
    return player:getMark("tea__chizong_ma-turn") == 0 or player:getMark("tea__chizong_mb-turn") == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected)
    if #selected == 0 and self.interaction.data then
      if self.interaction.data == "chizong_recover" then
        local tos = table.filter(Fk:currentRoom().alive_players, function(p)
          return table.every(Fk:currentRoom().alive_players, function(p1)
            return #p:getCardIds("h") >= #p1:getCardIds("h")
          end) and p:isWounded()
        end)
        return table.contains(tos, to_select)
      else
        local tos = table.filter(Fk:currentRoom().alive_players, function(p)
          return table.every(Fk:currentRoom().alive_players, function(p1)
            return p.hp >= p1.hp
          end)
        end)
        return table.contains(tos, to_select)
      end
    end
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local choice = self.interaction.data

    if choice == "chizong_recover" then
      room:setPlayerMark(player, "tea__chizong_ma-turn", 1)
      room:recover{
        who = target,
        num = 1,
        skillName = self.name,
      }
      room:setPlayerMark(target, "tea__chizong_a-turn", 1)
    else
      room:setPlayerMark(player, "tea__chizong_mb-turn", 1)
      target:drawCards(2, self.name)
      room:setPlayerMark(target, "tea__chizong_b-turn", 1)
    end

    if target:getMark("tea__chizong_a-turn") > 0 and target:getMark("tea__chizong_b-turn") > 0 and not target.dead then
      room:delay(500)
      local c = Fk:cloneCard("archery_attack")
      c.skillName = skill.name
      local use = {
        from = target,
        card = c,
      }
      if not player:prohibitUse(c) then
        room:useCard(use)
      end
    end
  end,
}
skill:addEffect("active", chizong)
return skill